home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / viper-util.el.z / viper-util.el
Encoding:
Text File  |  1998-10-28  |  41.8 KB  |  1,253 lines

  1. ;;; viper-util.el --- Utilities used by viper.el
  2.  
  3. ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  19. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. ;; Boston, MA 02111-1307, USA.
  21.  
  22.  
  23. ;; Code
  24.  
  25. (require 'ring)
  26.  
  27. ;; Compiler pacifier
  28. (defvar vip-overriding-map)
  29. (defvar pm-color-alist)
  30. (defvar zmacs-region-stays)
  31. (defvar vip-search-face)
  32. (defvar vip-minibuffer-current-face)
  33. (defvar vip-minibuffer-insert-face)
  34. (defvar vip-minibuffer-vi-face)
  35. (defvar vip-minibuffer-emacs-face)
  36. (defvar vip-replace-overlay-face)
  37. (defvar vip-minibuffer-overlay)
  38. (defvar vip-replace-overlay)
  39. (defvar vip-search-overlay)
  40. (defvar vip-replace-overlay-cursor-color)
  41. (defvar vip-intermediate-command)
  42. (defvar vip-use-replace-region-delimiters)
  43. (defvar vip-fast-keyseq-timeout)
  44. (defvar vip-related-files-and-buffers-ring)
  45. ;; end compiler pacifier
  46.  
  47. ;; Is it XEmacs?
  48. (defconst vip-xemacs-p (string-match "\\(Lucid\\|XEmacs\\)" emacs-version))
  49. ;; Is it Emacs?
  50. (defconst vip-emacs-p (not vip-xemacs-p))
  51. ;; Tell whether we are running as a window application or on a TTY
  52. (defsubst vip-device-type ()
  53.   (if vip-emacs-p
  54.       window-system
  55.     (device-type (selected-device))))
  56. ;; in XEmacs: device-type is tty on tty and stream in batch.
  57. (defun vip-window-display-p ()
  58.   (and (vip-device-type) (not (memq (vip-device-type) '(tty stream)))))
  59.  
  60. (defvar vip-ms-style-os-p (memq system-type '(ms-dos windows-nt windows-95))
  61.   "Tells if Emacs is running under an MS-style OS: ms-dos, windows-nt, W95.")
  62. (defvar vip-vms-os-p (memq system-type '(vax-vms axp-vms))
  63.   "Tells if Emacs is running under VMS.")
  64.  
  65. (defvar vip-force-faces nil
  66.   "If t, Viper will think that it is running on a display that supports faces.
  67. This is provided as a temporary relief for users of face-capable displays
  68. that Viper doesn't know about.")
  69.  
  70. (defun vip-has-face-support-p ()
  71.   (cond ((vip-window-display-p))
  72.     (vip-force-faces)
  73.     (vip-emacs-p (memq (vip-device-type) '(pc)))
  74.     (vip-xemacs-p (memq (vip-device-type) '(tty pc)))))
  75.  
  76.  
  77. ;;; Macros
  78.  
  79. (defmacro vip-deflocalvar (var default-value &optional documentation)
  80.   (` (progn
  81.        (defvar (, var) (, default-value)
  82.            (, (format "%s\n\(buffer local\)" documentation)))
  83.        (make-variable-buffer-local '(, var))
  84.      )))
  85.  
  86. (defmacro vip-loop (count body)
  87.   "(vip-loop COUNT BODY) Execute BODY COUNT times."
  88.   (list 'let (list (list 'count count))
  89.     (list 'while '(> count 0)
  90.           body
  91.           '(setq count (1- count))
  92.           )))
  93.  
  94. (defmacro vip-buffer-live-p (buf)
  95.   (` (and (, buf) (get-buffer (, buf)) (buffer-name (get-buffer (, buf))))))
  96.   
  97. ;; return buffer-specific macro definition, given a full macro definition
  98. (defmacro vip-kbd-buf-alist (macro-elt)
  99.   (` (nth 1 (, macro-elt))))
  100. ;; get a pair: (curr-buffer . macro-definition)
  101. (defmacro vip-kbd-buf-pair (macro-elt)
  102.   (` (assoc (buffer-name) (vip-kbd-buf-alist (, macro-elt)))))
  103. ;; get macro definition for current buffer
  104. (defmacro vip-kbd-buf-definition (macro-elt)
  105.   (` (cdr (vip-kbd-buf-pair (, macro-elt)))))
  106.   
  107. ;; return mode-specific macro definitions, given a full macro definition
  108. (defmacro vip-kbd-mode-alist (macro-elt)
  109.   (` (nth 2 (, macro-elt))))
  110. ;; get a pair: (major-mode . macro-definition)
  111. (defmacro vip-kbd-mode-pair (macro-elt)
  112.   (` (assoc major-mode (vip-kbd-mode-alist (, macro-elt)))))
  113. ;; get macro definition for the current major mode
  114. (defmacro vip-kbd-mode-definition (macro-elt)
  115.   (` (cdr (vip-kbd-mode-pair (, macro-elt)))))
  116.   
  117. ;; return global macro definition, given a full macro definition
  118. (defmacro vip-kbd-global-pair (macro-elt)
  119.   (` (nth 3 (, macro-elt))))
  120. ;; get global macro definition from an elt of macro-alist
  121. (defmacro vip-kbd-global-definition (macro-elt)
  122.   (` (cdr (vip-kbd-global-pair (, macro-elt)))))
  123.   
  124. ;; last elt of a sequence
  125. (defsubst vip-seq-last-elt (seq)
  126.   (elt seq (1- (length seq))))
  127.   
  128. ;; Check if arg is a valid character for register
  129. ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
  130. ;; Letter means lowercase letters, Letter means uppercase letters, and
  131. ;; digit means digits from 1 to 9.
  132. ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
  133. (defun vip-valid-register (reg &optional type)
  134.   (or type (setq type '(letter Letter digit)))
  135.   (or (if (memq 'letter type)
  136.       (and (<= ?a reg) (<= reg ?z)))
  137.       (if (memq 'digit type)
  138.       (and (<= ?1 reg) (<= reg ?9)))
  139.       (if (memq 'Letter type)
  140.       (and (<= ?A reg) (<= reg ?Z)))
  141.       ))
  142.       
  143. ;; checks if object is a marker, has a buffer, and points to within that buffer
  144. (defun vip-valid-marker (marker)
  145.   (if (and (markerp marker) (marker-buffer marker))
  146.       (let ((buf (marker-buffer marker))
  147.         (pos (marker-position marker)))
  148.     (save-excursion
  149.       (set-buffer buf)
  150.       (and (<= pos (point-max)) (<= (point-min) pos))))))
  151.   
  152.  
  153. (defvar vip-minibuffer-overlay-priority 300)
  154. (defvar vip-replace-overlay-priority 400)
  155. (defvar vip-search-overlay-priority 500)
  156.   
  157.  
  158. ;;; XEmacs support
  159.  
  160. (if vip-xemacs-p
  161.     (progn
  162.       (fset 'vip-read-event (symbol-function 'next-command-event))
  163.       (fset 'vip-make-overlay (symbol-function 'make-extent))
  164.       (fset 'vip-overlay-start (symbol-function 'extent-start-position))
  165.       (fset 'vip-overlay-end (symbol-function 'extent-end-position))
  166.       (fset 'vip-overlay-put (symbol-function 'set-extent-property))
  167.       (fset 'vip-overlay-p (symbol-function 'extentp))
  168.       (fset 'vip-overlay-get (symbol-function 'extent-property))
  169.       (fset 'vip-move-overlay (symbol-function 'set-extent-endpoints))
  170.       (if (vip-window-display-p)
  171.       (fset 'vip-iconify (symbol-function 'iconify-frame)))
  172.       (cond ((vip-has-face-support-p)
  173.          (fset 'vip-get-face (symbol-function 'get-face))
  174.          (fset 'vip-color-defined-p
  175.            (symbol-function 'valid-color-name-p))
  176.          )))
  177.   (fset 'vip-read-event (symbol-function 'read-event))
  178.   (fset 'vip-make-overlay (symbol-function 'make-overlay))
  179.   (fset 'vip-overlay-start (symbol-function 'overlay-start))
  180.   (fset 'vip-overlay-end (symbol-function 'overlay-end))
  181.   (fset 'vip-overlay-put (symbol-function 'overlay-put))
  182.   (fset 'vip-overlay-p (symbol-function 'overlayp))
  183.   (fset 'vip-overlay-get (symbol-function 'overlay-get))
  184.   (fset 'vip-move-overlay (symbol-function 'move-overlay))
  185.   (if (vip-window-display-p)
  186.       (fset 'vip-iconify (symbol-function 'iconify-or-deiconify-frame)))
  187.   (cond ((vip-has-face-support-p)
  188.      (fset 'vip-get-face (symbol-function 'internal-get-face))
  189.      (fset 'vip-color-defined-p (symbol-function 'x-color-defined-p))
  190.      )))
  191.  
  192. (fset 'vip-characterp
  193.       (symbol-function
  194.        (if vip-xemacs-p 'characterp 'integerp)))
  195.  
  196. (defsubst vip-color-display-p ()
  197.   (if vip-emacs-p
  198.       (x-display-color-p)
  199.     (eq (device-class (selected-device)) 'color)))
  200.    
  201. (defsubst vip-get-cursor-color ()
  202.   (if vip-emacs-p
  203.       (cdr (assoc 'cursor-color (frame-parameters)))
  204.     (color-instance-name (frame-property (selected-frame) 'cursor-color))))
  205.   
  206.   
  207. ;; OS/2
  208. (cond ((eq (vip-device-type) 'pm)
  209.        (fset 'vip-color-defined-p
  210.          (function (lambda (color) (assoc color pm-color-alist))))))
  211.     
  212. ;; needed to smooth out the difference between Emacs and XEmacs
  213. (defsubst vip-italicize-face (face)
  214.   (if vip-xemacs-p
  215.       (make-face-italic face)
  216.     (make-face-italic face nil 'noerror)))
  217.     
  218. ;; test if display is color and the colors are defined
  219. (defsubst vip-can-use-colors (&rest colors)
  220.   (if (vip-color-display-p)
  221.       (not (memq nil (mapcar 'vip-color-defined-p colors)))
  222.     ))
  223.  
  224. (defun vip-hide-face (face)
  225.   (if (and (vip-has-face-support-p) vip-emacs-p)
  226.       (add-to-list 'facemenu-unlisted-faces face)))
  227.  
  228. ;; cursor colors
  229. (defun vip-change-cursor-color (new-color)
  230.   (if (and (vip-window-display-p)  (vip-color-display-p)
  231.        (stringp new-color) (vip-color-defined-p new-color)
  232.        (not (string= new-color (vip-get-cursor-color))))
  233.       (modify-frame-parameters
  234.        (selected-frame) (list (cons 'cursor-color new-color)))))
  235.      
  236. (defsubst vip-save-cursor-color ()
  237.   (if (and (vip-window-display-p) (vip-color-display-p))
  238.       (let ((color (vip-get-cursor-color)))
  239.     (if (and (stringp color) (vip-color-defined-p color)
  240.          (not (string= color vip-replace-overlay-cursor-color)))
  241.         (vip-overlay-put vip-replace-overlay 'vip-cursor-color color)))))
  242.     
  243. (defsubst vip-restore-cursor-color ()
  244.   (vip-change-cursor-color
  245.    (vip-overlay-get vip-replace-overlay 'vip-cursor-color)))
  246.      
  247.  
  248. ;; Check the current version against the major and minor version numbers
  249. ;; using op: cur-vers op major.minor If emacs-major-version or
  250. ;; emacs-minor-version are not defined, we assume that the current version
  251. ;; is hopelessly outdated.  We assume that emacs-major-version and
  252. ;; emacs-minor-version are defined.  Otherwise, for Emacs/XEmacs 19, if the
  253. ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
  254. ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
  255. ;; incorrect. However, this gives correct result in our cases, since we are
  256. ;; testing for sufficiently high Emacs versions.
  257. (defun vip-check-version (op major minor &optional type-of-emacs)
  258.   (if (and (boundp 'emacs-major-version) (boundp 'emacs-minor-version))
  259.       (and (cond ((eq type-of-emacs 'xemacs) vip-xemacs-p)
  260.          ((eq type-of-emacs 'emacs) vip-emacs-p)
  261.          (t t))
  262.        (cond ((eq op '=) (and (= emacs-minor-version minor)
  263.                   (= emacs-major-version major)))
  264.          ((memq op '(> >= < <=))
  265.           (and (or (funcall op emacs-major-version major)
  266.                (= emacs-major-version major))
  267.                (if (= emacs-major-version major)
  268.                (funcall op emacs-minor-version minor)
  269.              t)))
  270.          (t
  271.           (error "%S: Invalid op in vip-check-version" op))))
  272.     (cond ((memq op '(= > >=)) nil)
  273.       ((memq op '(< <=)) t))))
  274.       
  275. ;;;; warn if it is a wrong version of emacs
  276. ;;(if (or (vip-check-version '< 19 29 'emacs)
  277. ;;    (vip-check-version '< 19 12 'xemacs))
  278. ;;    (progn
  279. ;;      (with-output-to-temp-buffer " *vip-info*"
  280. ;;    (switch-to-buffer " *vip-info*")
  281. ;;    (insert
  282. ;;     (format "
  283. ;;
  284. ;;This version of Viper requires 
  285. ;;
  286. ;;\t Emacs 19.29 and higher
  287. ;;\t OR
  288. ;;\t XEmacs 19.12 and higher
  289. ;;
  290. ;;It is unlikely to work under Emacs version %s
  291. ;;that you are using... " emacs-version))
  292. ;;
  293. ;;    (if noninteractive
  294. ;;        ()
  295. ;;      (beep 1)
  296. ;;      (beep 1)
  297. ;;      (insert "\n\nType any key to continue... ")
  298. ;;      (vip-read-event)))
  299. ;;      (kill-buffer " *vip-info*")))
  300.   
  301.  
  302. (defun vip-get-visible-buffer-window (wind)
  303.   (if vip-xemacs-p
  304.       (get-buffer-window wind t)
  305.     (get-buffer-window wind 'visible)))
  306.     
  307.     
  308. ;; Return line position.
  309. ;; If pos is 'start then returns position of line start.
  310. ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
  311. ;; Pos = 'indent returns beginning of indentation.
  312. ;; Otherwise, returns point. Current point is not moved in any case."
  313. (defun vip-line-pos (pos)
  314.   (let ((cur-pos (point))
  315.         (result))
  316.     (cond
  317.      ((equal pos 'start)
  318.       (beginning-of-line))
  319.      ((equal pos 'end)
  320.       (end-of-line))
  321.      ((equal pos 'mid)
  322.       (goto-char (+ (vip-line-pos 'start) (vip-line-pos 'end) 2)))
  323.      ((equal pos 'indent)
  324.       (back-to-indentation))
  325.      (t   nil))
  326.     (setq result (point))
  327.     (goto-char cur-pos)
  328.     result))
  329.  
  330.  
  331. ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
  332. ;; The first argument must eval to a variable name.
  333. ;; Arguments: (var-name position &optional buffer).
  334. ;; 
  335. ;; This is useful for moving markers that are supposed to be local.
  336. ;; For this, VAR-NAME should be made buffer-local with nil as a default.
  337. ;; Then, each time this var is used in `vip-move-marker-locally' in a new
  338. ;; buffer, a new marker will be created.
  339. (defun vip-move-marker-locally (var pos &optional buffer)
  340.   (if (markerp (eval var))
  341.       ()
  342.     (set var (make-marker)))
  343.   (move-marker (eval var) pos buffer))
  344.  
  345.  
  346. ;; Print CONDITIONS as a message.
  347. (defun vip-message-conditions (conditions)
  348.   (let ((case (car conditions)) (msg (cdr conditions)))
  349.     (if (null msg)
  350.     (message "%s" case)
  351.       (message "%s: %s" case (mapconcat 'prin1-to-string msg " ")))
  352.     (beep 1)))
  353.  
  354.  
  355.  
  356. ;;; List/alist utilities
  357.     
  358. ;; Convert LIST to an alist
  359. (defun vip-list-to-alist (lst)
  360.   (let ((alist))
  361.     (while lst
  362.       (setq alist (cons (list (car lst)) alist))
  363.       (setq lst (cdr lst)))
  364.     alist))    
  365.  
  366. ;; Convert ALIST to a list.
  367. (defun vip-alist-to-list (alst)
  368.   (let ((lst))
  369.     (while alst
  370.       (setq lst (cons (car (car alst)) lst))
  371.       (setq alst (cdr alst)))
  372.     lst))
  373.  
  374. ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
  375. (defun vip-filter-alist (regexp alst)
  376.   (interactive "s x")
  377.   (let ((outalst) (inalst alst))
  378.     (while (car inalst)
  379.       (if (string-match regexp (car (car inalst)))
  380.       (setq outalst (cons (car inalst) outalst)))
  381.       (setq inalst (cdr inalst)))
  382.     outalst))    
  383.        
  384. ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
  385. (defun vip-filter-list (regexp lst)
  386.   (interactive "s x")
  387.   (let ((outlst) (inlst lst))
  388.     (while (car inlst)
  389.       (if (string-match regexp (car inlst))
  390.       (setq outlst (cons (car inlst) outlst)))
  391.       (setq inlst (cdr inlst)))
  392.     outlst))    
  393.  
  394.    
  395. ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
  396. ;; LIS2 is modified by filtering it: deleting its members of the form
  397. ;; \(car elt\) such that (car elt') is in LIS1.
  398. (defun vip-append-filter-alist (lis1 lis2)
  399.   (let ((temp lis1)
  400.     elt)
  401.   
  402.     ;;filter-append the second list
  403.     (while temp
  404.       ;; delete all occurrences
  405.       (while (setq elt (assoc (car (car temp)) lis2))
  406.     (setq lis2 (delq elt lis2)))
  407.       (setq temp (cdr temp)))
  408.     
  409.     (nconc lis1 lis2)))
  410.  
  411.  
  412. ;;; Support for :e and file globbing
  413.  
  414. (defun vip-ex-nontrivial-find-file-unix (filespec)
  415.   "Glob the file spec and visit all files matching the spec.
  416. This function is designed to work under Unix. It may also work under VMS.
  417.  
  418. Users who prefer other types of shells should write their own version of this
  419. function and set the variable `ex-nontrivial-find-file-function'
  420. appropriately." 
  421.   (let ((gshell
  422.      (cond (ex-unix-type-shell shell-file-name)
  423.            ((memq system-type '(vax-vms axp-vms)) "*dcl*") ; VAX VMS
  424.            (t "sh"))) ; probably Unix anyway
  425.     (gshell-options
  426.      ;; using cond in anticipation of further additions
  427.      (cond (ex-unix-type-shell-options)
  428.            ))
  429.     (command (cond (vip-ms-style-os-p (format "\"ls -1 %s\"" filespec))
  430.                (t (format "ls -1 %s" filespec))))
  431.     file-list)
  432.     (save-excursion 
  433.       (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
  434.       (erase-buffer)
  435.       (setq status
  436.         (if gshell-options
  437.         (call-process gshell nil t nil
  438.                   gshell-options
  439.                   "-c"
  440.                   command)
  441.           (call-process gshell nil t nil
  442.                 "-c"
  443.                 command)))
  444.       (goto-char (point-min))
  445.       ;; Issue an error, if no match.
  446.       (if (> status 0)
  447.       (save-excursion
  448.         (skip-chars-forward " \t\n\j")
  449.         (if (looking-at "ls:")
  450.         (vip-forward-Word 1))
  451.         (error "%s: %s"
  452.            (if (stringp  gshell)
  453.                gshell
  454.              "shell")
  455.            (buffer-substring (point) (vip-line-pos 'end)))
  456.         ))
  457.       (goto-char (point-min))
  458.       (setq file-list (vip-get-filenames-from-buffer 'one-per-line)))
  459.  
  460.     (mapcar 'find-file file-list)
  461.     ))
  462.  
  463. (defun vip-ex-nontrivial-find-file-ms (filespec)
  464.   "Glob the file spec and visit all files matching the spec.
  465. This function is designed to work under MS type systems, such as NT, W95, and
  466. DOS. It may also work under OS/2.
  467.  
  468. The users of Unix-type shells should be able to use
  469. `vip-ex-nontrivial-find-file-unix', making it into the value of the variable 
  470. `ex-nontrivial-find-file-function'. If this doesn't work, the user may have
  471. to write a custom function, similar to `vip-ex-nontrivial-find-file-unix'."
  472.   (save-excursion 
  473.     (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
  474.     (erase-buffer)
  475.     (insert filespec)
  476.     (goto-char (point-min))
  477.     (mapcar 'find-file
  478.         (vip-glob-ms-windows-files (vip-get-filenames-from-buffer)))
  479.     ))
  480.  
  481.  
  482. ;; Interpret the stuff in the buffer as a list of file names
  483. ;; return a list of file names listed in the buffer beginning at point
  484. ;; If optional arg is supplied, assume each filename is listed on a separate
  485. ;; line
  486. (defun vip-get-filenames-from-buffer (&optional one-per-line)
  487.   (let ((skip-chars (if one-per-line "\t\n" " \t\n"))
  488.      result fname delim)
  489.     (skip-chars-forward skip-chars)
  490.     (while (not (eobp))
  491.       (if (cond ((looking-at "\"")
  492.          (setq delim ?\")
  493.          (re-search-forward "[^\"]+" nil t)) ; noerror
  494.         ((looking-at "'")
  495.          (setq delim ?')
  496.          (re-search-forward "[^']+" nil t)) ; noerror
  497.         (t 
  498.          (re-search-forward
  499.           (concat "[^" skip-chars "]+") nil t))) ;noerror
  500.       (setq fname
  501.         (buffer-substring (match-beginning 0) (match-end 0))))
  502.       (if delim
  503.       (forward-char 1))
  504.       (skip-chars-forward " \t\n")
  505.       (setq result (cons fname result)))
  506.     result))
  507.  
  508. ;; convert MS-DOS wildcards to regexp
  509. (defun vip-wildcard-to-regexp (wcard)
  510.   (save-excursion
  511.     (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
  512.     (erase-buffer)
  513.     (insert wcard)
  514.     (goto-char (point-min))
  515.     (while (not (eobp))
  516.       (skip-chars-forward "^*?.\\\\")
  517.       (cond ((eq (char-after (point)) ?*) (insert ".")(forward-char 1))
  518.         ((eq (char-after (point)) ?.) (insert "\\")(forward-char 1))
  519.         ((eq (char-after (point)) ?\\) (insert "\\")(forward-char 1))
  520.         ((eq (char-after (point)) ??) (delete-char 1)(insert ".")))
  521.       )
  522.     (buffer-string)
  523.     ))
  524.  
  525.  
  526. ;; glob windows files
  527. ;; LIST is expected to be in reverse order
  528. (defun vip-glob-ms-windows-files (list)
  529.   (let ((tmp list)
  530.     (case-fold-search t)
  531.     tmp2)
  532.     (while tmp
  533.       (setq tmp2 (cons (directory-files 
  534.             ;; the directory part
  535.             (or (file-name-directory (car tmp))
  536.                 "")
  537.             t  ; return full names
  538.             ;; the regexp part: globs the file names
  539.             (concat "^"
  540.                 (vip-wildcard-to-regexp
  541.                  (file-name-nondirectory (car tmp)))
  542.                 "$"))
  543.                tmp2))
  544.       (setq tmp (cdr tmp)))
  545.     (reverse (apply 'append tmp2))))
  546.  
  547.       
  548.  
  549.  
  550.  
  551. ;;; Insertion ring
  552.  
  553. ;; Rotate RING's index. DIRection can be positive or negative.
  554. (defun vip-ring-rotate1 (ring dir)
  555.   (if (and (ring-p ring) (> (ring-length ring) 0))
  556.       (progn
  557.     (setcar ring (cond ((> dir 0)
  558.                 (ring-plus1 (car ring) (ring-length ring)))
  559.                ((< dir 0)
  560.                 (ring-minus1 (car ring) (ring-length ring)))
  561.                ;; don't rotate if dir = 0
  562.                (t (car ring))))
  563.     (vip-current-ring-item ring)
  564.     )))
  565.     
  566. (defun vip-special-ring-rotate1 (ring dir)
  567.   (if (memq vip-intermediate-command
  568.         '(repeating-display-destructive-command
  569.           repeating-insertion-from-ring))
  570.       (vip-ring-rotate1 ring dir)
  571.     ;; don't rotate otherwise
  572.     (vip-ring-rotate1 ring 0)))
  573.     
  574. ;; current ring item; if N is given, then so many items back from the
  575. ;; current
  576. (defun vip-current-ring-item (ring &optional n)
  577.   (setq n (or n 0))
  578.   (if (and (ring-p ring) (> (ring-length ring) 0))
  579.       (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring)))))
  580.     
  581. ;; push item onto ring. the second argument is a ring-variable, not value.
  582. (defun vip-push-onto-ring (item ring-var)
  583.   (or (ring-p (eval ring-var))
  584.       (set ring-var (make-ring (eval (intern (format "%S-size" ring-var))))))
  585.   (or (null item) ; don't push nil
  586.       (and (stringp item) (string= item "")) ; or empty strings
  587.       (equal item (vip-current-ring-item (eval ring-var))) ; or old stuff
  588.       ;; Since vip-set-destructive-command checks if we are inside vip-repeat,
  589.       ;;    we don't check whether this-command-keys is a `.'.
  590.       ;;    The cmd vip-repeat makes a call to the current function only if
  591.       ;;    `.' is executing a command from the command history. It doesn't
  592.       ;;    call the push-onto-ring function if `.' is simply repeating the
  593.       ;;    last destructive command.
  594.       ;; We only check for ESC (which happens when we do insert with a
  595.       ;;    prefix argument, or if this-command-keys doesn't give anything
  596.       ;;    meaningful (in that case we don't know what to show to the user).
  597.       (and (eq ring-var 'vip-command-ring)
  598.        (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
  599.              (vip-array-to-string (this-command-keys))))
  600.       (vip-ring-insert (eval ring-var) item))
  601.   )
  602.   
  603.  
  604. ;; removing elts from ring seems to break it
  605. (defun vip-cleanup-ring (ring)
  606.   (or (< (ring-length ring) 2)
  607.       (null (vip-current-ring-item ring))
  608.       ;; last and previous equal
  609.       (if (equal (vip-current-ring-item ring) (vip-current-ring-item ring 1))
  610.       (vip-ring-pop ring))))
  611.       
  612. ;; ring-remove seems to be buggy, so we concocted this for our purposes.
  613. (defun vip-ring-pop (ring)
  614.   (let* ((ln (ring-length ring))
  615.      (vec (cdr (cdr ring)))
  616.      (veclen (length vec))
  617.      (hd (car ring))
  618.      (idx (max 0 (ring-minus1 hd ln)))
  619.      (top-elt (aref vec idx)))
  620.     
  621.     ;; shift elements
  622.     (while (< (1+ idx) veclen)
  623.       (aset vec idx (aref vec (1+ idx)))
  624.       (setq idx (1+ idx)))
  625.     (aset vec idx nil)
  626.     
  627.     (setq hd (max 0 (ring-minus1 hd ln)))
  628.     (if (= hd (1- ln)) (setq hd 0))
  629.     (setcar ring hd) ; move head
  630.     (setcar (cdr ring) (max 0 (1- ln))) ; adjust length
  631.     top-elt
  632.     ))
  633.     
  634. (defun vip-ring-insert (ring item)
  635.   (let* ((ln (ring-length ring))
  636.      (vec (cdr (cdr ring)))
  637.      (veclen (length vec))
  638.      (hd (car ring))
  639.      (vecpos-after-hd (if (= hd 0) ln hd))
  640.      (idx ln))
  641.      
  642.     (if (= ln veclen)
  643.     (progn
  644.       (aset vec hd item) ; hd is always 1+ the actual head index in vec
  645.       (setcar ring (ring-plus1 hd ln)))
  646.       (setcar (cdr ring) (1+ ln))
  647.       (setcar ring (ring-plus1 vecpos-after-hd (1+ ln)))
  648.       (while (and (>= idx vecpos-after-hd) (> ln 0))
  649.     (aset vec idx (aref vec (1- idx)))
  650.     (setq idx (1- idx)))
  651.       (aset vec vecpos-after-hd item))
  652.     item))
  653.     
  654.  
  655. ;;; String utilities
  656.  
  657. ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
  658. ;; PRE-STRING is a string to prepend to the abbrev string.
  659. ;; POST-STRING is a string to append to the abbrev string.
  660. ;; ABBREV_SIGN is a string to be inserted before POST-STRING
  661. ;; if the orig string was truncated. 
  662. (defun vip-abbreviate-string (string max-len
  663.                      pre-string post-string abbrev-sign)
  664.   (let (truncated-str)
  665.     (setq truncated-str
  666.       (if (stringp string) 
  667.           (substring string 0 (min max-len (length string)))))
  668.     (cond ((null truncated-str) "")
  669.       ((> (length string) max-len)
  670.        (format "%s%s%s%s"
  671.            pre-string truncated-str abbrev-sign post-string))
  672.       (t (format "%s%s%s" pre-string truncated-str post-string)))))
  673.  
  674. ;; tells if we are over a whitespace-only line
  675. (defsubst vip-over-whitespace-line ()
  676.   (save-excursion
  677.     (beginning-of-line)
  678.     (looking-at "^[ \t]*$")))
  679.       
  680.  
  681. ;;; Saving settings in custom file
  682.  
  683. ;; Save the current setting of VAR in CUSTOM-FILE.
  684. ;; If given, MESSAGE is a message to be displayed after that.
  685. ;; This message is erased after 2 secs, if erase-msg is non-nil.
  686. ;; Arguments: var message custom-file &optional erase-message
  687. (defun vip-save-setting (var message custom-file &optional erase-msg)
  688.   (let* ((var-name (symbol-name var))
  689.      (var-val (if (boundp var) (eval var)))
  690.      (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name))
  691.      (buf (find-file-noselect (substitute-in-file-name custom-file)))
  692.     )
  693.     (message message)
  694.     (save-excursion
  695.       (set-buffer buf)
  696.       (goto-char (point-min))
  697.       (if (re-search-forward regexp nil t)
  698.       (let ((reg-end (1- (match-end 0))))
  699.         (search-backward var-name)
  700.         (delete-region (match-beginning 0) reg-end)
  701.         (goto-char (match-beginning 0))
  702.         (insert (format "%s  '%S" var-name var-val)))
  703.     (goto-char (point-max))
  704.     (if (not (bolp)) (insert "\n"))
  705.     (insert (format "(setq %s '%S)\n" var-name var-val)))
  706.       (save-buffer))
  707.       (kill-buffer buf)
  708.       (if erase-msg
  709.       (progn
  710.         (sit-for 2)
  711.         (message "")))
  712.       ))
  713.       
  714. ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
  715. ;; match this pattern.
  716. (defun vip-save-string-in-file (string custom-file &optional pattern)
  717.   (let ((buf (find-file-noselect (substitute-in-file-name custom-file))))
  718.     (save-excursion
  719.       (set-buffer buf)
  720.       (goto-char (point-min))
  721.       (if pattern (delete-matching-lines pattern))
  722.       (goto-char (point-max))
  723.       (if string (insert string))
  724.       (save-buffer))
  725.     (kill-buffer buf)
  726.     ))
  727.     
  728.  
  729. ;;; Overlays
  730.  
  731. ;; Search
  732.  
  733. (defun vip-flash-search-pattern ()
  734.   (if (vip-overlay-p vip-search-overlay)
  735.       (vip-move-overlay vip-search-overlay (match-beginning 0) (match-end 0))
  736.     (setq vip-search-overlay
  737.       (vip-make-overlay
  738.        (match-beginning 0) (match-end 0) (current-buffer))))
  739.   
  740.   (vip-overlay-put vip-search-overlay 'priority vip-search-overlay-priority)
  741.   (if (vip-has-face-support-p)
  742.       (progn
  743.     (vip-overlay-put vip-search-overlay 'face vip-search-face)
  744.     (sit-for 2)
  745.     (vip-overlay-put vip-search-overlay 'face nil))))
  746.  
  747. ;; Replace state
  748.  
  749. (defsubst vip-move-replace-overlay (beg end)
  750.   (vip-move-overlay vip-replace-overlay beg end))
  751.   
  752. (defun vip-set-replace-overlay (beg end)
  753.   (if (vip-overlay-p vip-replace-overlay)
  754.       (vip-move-replace-overlay beg end)
  755.     (setq vip-replace-overlay (vip-make-overlay beg end (current-buffer)))
  756.     ;; never detach
  757.     (vip-overlay-put
  758.      vip-replace-overlay (if vip-emacs-p 'evaporate 'detachable) nil)
  759.     (vip-overlay-put 
  760.      vip-replace-overlay 'priority vip-replace-overlay-priority)) 
  761.   (if (vip-has-face-support-p)
  762.       (vip-overlay-put vip-replace-overlay 'face vip-replace-overlay-face))
  763.   (vip-save-cursor-color)
  764.   (vip-change-cursor-color vip-replace-overlay-cursor-color)
  765.   )
  766.   
  767.       
  768. (defsubst vip-set-replace-overlay-glyphs (before-glyph after-glyph)
  769.   (if (or (not (vip-has-face-support-p))
  770.       vip-use-replace-region-delimiters)
  771.       (let ((before-name (if vip-xemacs-p 'begin-glyph 'before-string))
  772.         (after-name (if vip-xemacs-p 'end-glyph 'after-string)))
  773.     (vip-overlay-put vip-replace-overlay before-name before-glyph)
  774.     (vip-overlay-put vip-replace-overlay after-name after-glyph))))
  775.   
  776. (defsubst vip-hide-replace-overlay ()
  777.   (vip-set-replace-overlay-glyphs nil nil)
  778.   (vip-restore-cursor-color)
  779.   (if (vip-has-face-support-p)
  780.       (vip-overlay-put vip-replace-overlay 'face nil)))
  781.  
  782.     
  783. (defsubst vip-replace-start ()
  784.   (vip-overlay-start vip-replace-overlay))
  785. (defsubst vip-replace-end ()
  786.   (vip-overlay-end vip-replace-overlay))
  787.  
  788.  
  789. ;; Minibuffer
  790.  
  791. (defun vip-set-minibuffer-overlay ()
  792.   (vip-check-minibuffer-overlay)
  793.   (if (vip-has-face-support-p)
  794.       (progn
  795.     (vip-overlay-put
  796.      vip-minibuffer-overlay 'face vip-minibuffer-current-face)
  797.     (vip-overlay-put 
  798.      vip-minibuffer-overlay 'priority vip-minibuffer-overlay-priority)
  799.     ;; never detach
  800.     (vip-overlay-put
  801.      vip-minibuffer-overlay (if vip-emacs-p 'evaporate 'detachable) nil)
  802.     ;; make vip-minibuffer-overlay open-ended
  803.     ;; In emacs, it is made open ended at creation time
  804.     (if vip-xemacs-p
  805.         (progn
  806.           (vip-overlay-put vip-minibuffer-overlay 'start-open nil)
  807.           (vip-overlay-put vip-minibuffer-overlay 'end-open nil)))
  808.     )))
  809.        
  810. (defun vip-check-minibuffer-overlay ()
  811.   (or (vip-overlay-p vip-minibuffer-overlay)
  812.       (setq vip-minibuffer-overlay
  813.         (if vip-xemacs-p
  814.         (vip-make-overlay 1 (1+ (buffer-size)) (current-buffer))
  815.           ;; make overlay open-ended
  816.           (vip-make-overlay
  817.            1 (1+ (buffer-size)) (current-buffer) nil 'rear-advance)))
  818.       ))
  819.  
  820.  
  821. (defsubst vip-is-in-minibuffer ()
  822.   (string-match "\*Minibuf-" (buffer-name)))
  823.   
  824.  
  825.  
  826. ;;; XEmacs compatibility
  827.  
  828. (defun vip-abbreviate-file-name (file)
  829.   (if vip-emacs-p
  830.       (abbreviate-file-name file)
  831.     ;; XEmacs requires addl argument
  832.     (abbreviate-file-name file t)))
  833.     
  834. ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg 
  835. ;; in sit-for, so this function smoothes out the differences.
  836. (defsubst vip-sit-for-short (val &optional nodisp)
  837.   (if vip-xemacs-p
  838.       (sit-for (/ val 1000.0) nodisp)
  839.     (sit-for 0 val nodisp)))
  840.  
  841. ;; EVENT may be a single event of a sequence of events
  842. (defsubst vip-ESC-event-p (event)
  843.   (let ((ESC-keys '(?\e (control \[) escape))
  844.     (key (vip-event-key event)))
  845.     (member key ESC-keys)))
  846.     
  847.   
  848. (defsubst vip-mark-marker ()
  849.   (if vip-xemacs-p
  850.       (mark-marker t)
  851.     (mark-marker)))
  852.  
  853. ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
  854. ;; is the same as (mark t).
  855. (defsubst vip-set-mark-if-necessary ()
  856.   (setq mark-ring (delete (vip-mark-marker) mark-ring))
  857.   (set-mark-command nil))
  858.        
  859. ;; In transient mark mode (zmacs mode), it is annoying when regions become
  860. ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
  861. ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
  862. (defun vip-deactivate-mark ()
  863.   (if vip-xemacs-p
  864.       (zmacs-deactivate-region)
  865.     (deactivate-mark)))
  866.  
  867. (defsubst vip-leave-region-active ()
  868.   (if vip-xemacs-p
  869.       (setq zmacs-region-stays t)))
  870.  
  871.     
  872. (defsubst vip-events-to-keys (events)
  873.   (cond (vip-xemacs-p (events-to-keys events))
  874.     (t events)))
  875.           
  876.     
  877. (defun vip-eval-after-load (file form)
  878.   (if vip-emacs-p
  879.       (eval-after-load file form)
  880.     (or (assoc file after-load-alist)
  881.     (setq after-load-alist (cons (list file) after-load-alist)))
  882.     (let ((elt (assoc file after-load-alist)))
  883.       (or (member form (cdr elt))
  884.       (setq elt (nconc elt (list form)))))
  885.     form
  886.     ))
  887.  
  888. ;; This is here because Emacs changed the way local hooks work.
  889. ;;
  890. ;;Add to the value of HOOK the function FUNCTION.
  891. ;;FUNCTION is not added if already present.
  892. ;;FUNCTION is added (if necessary) at the beginning of the hook list
  893. ;;unless the optional argument APPEND is non-nil, in which case
  894. ;;FUNCTION is added at the end.
  895. ;;
  896. ;;HOOK should be a symbol, and FUNCTION may be any valid function.  If
  897. ;;HOOK is void, it is first set to nil.  If HOOK's value is a single
  898. ;;function, it is changed to a list of functions."
  899. (defun vip-add-hook (hook function &optional append)
  900.   (if (not (boundp hook)) (set hook nil))
  901.   ;; If the hook value is a single function, turn it into a list.
  902.   (let ((old (symbol-value hook)))
  903.     (if (or (not (listp old)) (eq (car old) 'lambda))
  904.     (setq old (list old)))
  905.     (if (member function old)
  906.     nil
  907.       (set hook (if append
  908.             (append old (list function)) ; don't nconc
  909.           (cons function old))))))
  910.  
  911. ;; This is here because of Emacs's changes in the semantics of add/remove-hooks
  912. ;; and due to the bugs they introduced.
  913. ;;
  914. ;; Remove from the value of HOOK the function FUNCTION.
  915. ;; HOOK should be a symbol, and FUNCTION may be any valid function.  If
  916. ;; FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
  917. ;; list of hooks to run in HOOK, then nothing is done.  See `vip-add-hook'."
  918. (defun vip-remove-hook (hook function)
  919.   (if (or (not (boundp hook))        ;unbound symbol, or
  920.       (null (symbol-value hook))    ;value is nil, or
  921.       (null function))        ;function is nil, then
  922.       nil                ;Do nothing.
  923.     (let ((hook-value (symbol-value hook)))
  924.       (if (consp hook-value)
  925.       ;; don't side-effect the list
  926.       (setq hook-value (delete function (copy-sequence hook-value)))
  927.     (if (equal hook-value function)
  928.         (setq hook-value nil)))
  929.       (set hook hook-value))))
  930.  
  931.     
  932.     
  933. ;; like read-event, but in XEmacs also try to convert to char, if possible
  934. (defun vip-read-event-convert-to-char ()
  935.   (let (event)
  936.     (if vip-emacs-p
  937.     (read-event)
  938.       (setq event (next-command-event))
  939.       (or (event-to-character event)
  940.       event))
  941.     ))
  942.  
  943. ;; This function lets function-key-map convert key sequences into logical
  944. ;; keys. This does a better job than vip-read-event when it comes to kbd
  945. ;; macros, since it enables certain macros to be shared between X and TTY modes
  946. ;; by correctly mapping key sequences for Left/Right/... (one an ascii
  947. ;; terminal) into logical keys left, right, etc.
  948. (defun vip-read-key () 
  949.   (let ((overriding-local-map vip-overriding-map) 
  950.     (inhibit-quit t)
  951.         key) 
  952.     (use-global-map vip-overriding-map) 
  953.     (setq key (elt (read-key-sequence nil) 0)) 
  954.     (use-global-map global-map) 
  955.     key))
  956.  
  957.  
  958. ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
  959. ;; instead of nil, if '(nil) was previously inadvertently assigned to
  960. ;; unread-command-events
  961. (defun vip-event-key (event)
  962.   (or (and event (eventp event))
  963.       (error "vip-event-key: Wrong type argument, eventp, %S" event))
  964.   (let ((mod (event-modifiers event))
  965.     basis)
  966.     (setq basis
  967.       (cond
  968.        (vip-xemacs-p
  969.         (cond ((key-press-event-p event)
  970.            (event-key event))
  971.           ((button-event-p event)
  972.            (concat "mouse-" (prin1-to-string (event-button event))))
  973.           (t 
  974.            (error "vip-event-key: Unknown event, %S" event))))
  975.        (t 
  976.         ;; Emacs doesn't handle capital letters correctly, since
  977.         ;; \S-a isn't considered the same as A (it behaves as
  978.         ;; plain `a' instead). So we take care of this here
  979.         (cond ((and (vip-characterp event) (<= ?A event) (<= event ?Z))
  980.            (setq mod nil
  981.              event event))
  982.           ;; Emacs has the oddity whereby characters 128+char
  983.           ;; represent M-char *if* this appears inside a string.
  984.           ;; So, we convert them manually to (meta char).
  985.           ((and (vip-characterp event) (< ?\C-? event) (<= event 255))
  986.            (setq mod '(meta)
  987.              event (- event ?\C-? 1)))
  988.           (t (event-basic-type event)))
  989.         )))
  990.     (if (vip-characterp basis)
  991.     (setq basis
  992.           (if (= basis ?\C-?)
  993.           (list 'control '\?) ; taking care of an emacs bug
  994.         (intern (char-to-string basis)))))
  995.     (if mod
  996.     (append mod (list basis))
  997.       basis)))
  998.     
  999. (defun vip-key-to-emacs-key (key)
  1000.   (let (key-name char-p modifiers mod-char-list base-key base-key-name)
  1001.     (cond (vip-xemacs-p key)
  1002.       ((symbolp key)
  1003.        (setq key-name (symbol-name key))
  1004.        (if (= (length key-name) 1) ; character event
  1005.            (string-to-char key-name)
  1006.          key))
  1007.       ((listp key)
  1008.        (setq modifiers (subseq key 0 (1- (length key)))
  1009.          base-key (vip-seq-last-elt key)
  1010.          base-key-name (symbol-name base-key)
  1011.          char-p (= (length base-key-name) 1))
  1012.        (setq mod-char-list
  1013.          (mapcar
  1014.           '(lambda (elt) (upcase (substring (symbol-name elt) 0 1)))
  1015.           modifiers))
  1016.        (if char-p
  1017.            (setq key-name
  1018.              (car (read-from-string
  1019.                (concat
  1020.                 "?\\"
  1021.                 (mapconcat 'identity mod-char-list "-\\")
  1022.                 "-"
  1023.                 base-key-name))))
  1024.          (setq key-name
  1025.            (intern
  1026.             (concat
  1027.              (mapconcat 'identity mod-char-list "-")
  1028.              "-"
  1029.              base-key-name))))))
  1030.     ))
  1031.  
  1032.  
  1033. ;; Args can be a sequence of events, a string, or a Viper macro.  Will try to
  1034. ;; convert events to keys and, if all keys are regular printable
  1035. ;; characters, will return a string. Otherwise, will return a string
  1036. ;; representing a vector of converted events. If the input was a Viper macro,
  1037. ;; will return a string that represents this macro as a vector.
  1038. (defun vip-array-to-string (event-seq)
  1039.   (let (temp temp2)
  1040.     (cond ((stringp event-seq) event-seq)
  1041.       ((vip-event-vector-p event-seq)
  1042.         (setq temp (mapcar 'vip-event-key event-seq))
  1043.         (cond ((vip-char-symbol-sequence-p temp)
  1044.            (mapconcat 'symbol-name temp ""))
  1045.           ((and (vip-char-array-p
  1046.              (setq temp2 (mapcar 'vip-key-to-character temp))))
  1047.            (mapconcat 'char-to-string temp2 ""))
  1048.           (t (prin1-to-string (vconcat temp)))))
  1049.       ((vip-char-symbol-sequence-p event-seq)
  1050.        (mapconcat 'symbol-name event-seq ""))
  1051.       ((and (vectorp event-seq) 
  1052.         (vip-char-array-p
  1053.          (setq temp (mapcar 'vip-key-to-character event-seq))))
  1054.        (mapconcat 'char-to-string temp ""))
  1055.       (t (prin1-to-string event-seq)))))
  1056.  
  1057. (defun vip-key-press-events-to-chars (events)
  1058.   (mapconcat (if vip-emacs-p
  1059.          'char-to-string
  1060.            (function
  1061.         (lambda (elt) (char-to-string (event-to-character elt)))))
  1062.          events
  1063.          ""))
  1064.        
  1065.     
  1066. (defsubst vip-fast-keysequence-p ()
  1067.   (not (vip-sit-for-short vip-fast-keyseq-timeout t)))
  1068.     
  1069. (defun vip-read-char-exclusive ()
  1070.   (let (char
  1071.     (echo-keystrokes 1))
  1072.     (while (null char)
  1073.       (condition-case nil
  1074.       (setq char (read-char))
  1075.     (error
  1076.      ;; skip event if not char
  1077.      (vip-read-event))))
  1078.     char))
  1079.  
  1080. ;; key is supposed to be in viper's representation, e.g., (control l), a
  1081. ;; character, etc.
  1082. (defun vip-key-to-character (key)
  1083.   (cond ((eq key 'space) ?\ )
  1084.     ((eq key 'delete) ?\C-?)
  1085.     ((eq key 'backspace) ?\C-h)
  1086.     ((and (symbolp key)
  1087.           (= 1 (length (symbol-name key))))
  1088.      (string-to-char (symbol-name key)))
  1089.     ((and (listp key)
  1090.           (eq (car key) 'control)
  1091.           (symbol-name (nth 1 key))
  1092.           (= 1 (length (symbol-name (nth 1 key)))))
  1093.      (read (format "?\\C-%s" (symbol-name (nth 1 key)))))
  1094.     (t key)))
  1095.     
  1096.       
  1097. (defun vip-setup-master-buffer (&rest other-files-or-buffers)
  1098.   "Set up the current buffer as a master buffer.
  1099. Arguments become related buffers. This function should normally be used in
  1100. the `Local variables' section of a file."
  1101.   (setq vip-related-files-and-buffers-ring 
  1102.     (make-ring (1+ (length other-files-or-buffers))))
  1103.   (mapcar '(lambda (elt)
  1104.          (vip-ring-insert vip-related-files-and-buffers-ring elt))
  1105.       other-files-or-buffers)
  1106.   (vip-ring-insert vip-related-files-and-buffers-ring (buffer-name))
  1107.   )
  1108.  
  1109. ;;; Movement utilities
  1110.  
  1111. (defvar vip-syntax-preference 'strict-vi
  1112.   "*Syntax type characterizing Viper's alphanumeric symbols.
  1113. `emacs' means only word constituents are considered to be alphanumeric.
  1114. Word constituents are symbols specified as word constituents by the current
  1115. syntax table.
  1116. `extended' means word and symbol constituents.
  1117. `reformed-vi' means Vi-ish behavior: word constituents and the symbol `_'.
  1118. However, word constituents are determined according to Emacs syntax tables,
  1119. which may be different from Vi in some major modes.
  1120. `strict-vi' means Viper words are exactly as in Vi.")
  1121.  
  1122. (vip-deflocalvar vip-ALPHA-char-class "w"
  1123.   "String of syntax classes characterizing Viper's alphanumeric symbols.
  1124. In addition, the symbol `_' may be considered alphanumeric if
  1125. `vip-syntax-preference'is `reformed-vi'.")
  1126.  
  1127. (vip-deflocalvar vip-strict-ALPHA-chars "a-zA-Z0-9_"
  1128.   "Regexp matching the set of alphanumeric characters acceptable to strict
  1129. Vi.")
  1130. (vip-deflocalvar vip-strict-SEP-chars " \t\n"
  1131.   "Regexp matching the set of alphanumeric characters acceptable to strict
  1132. Vi.")
  1133.  
  1134. (vip-deflocalvar vip-SEP-char-class " -"
  1135.   "String of syntax classes for Vi separators.
  1136. Usually contains ` ', linefeed, TAB or formfeed.")
  1137.  
  1138. (defun vip-update-alphanumeric-class ()
  1139.   "Set the syntactic class of Viper alphanumeric symbols according to
  1140. the variable `vip-ALPHA-char-class'. Should be called in order for changes to
  1141. `vip-ALPHA-char-class' to take effect."
  1142.   (interactive)
  1143.   (setq-default
  1144.    vip-ALPHA-char-class
  1145.    (cond ((eq vip-syntax-preference 'emacs) "w")     ; only word constituents
  1146.      ((eq vip-syntax-preference 'extended) "w_") ; word & symbol chars
  1147.      (t "w"))))     ; vi syntax: word constituents and the symbol `_'
  1148.  
  1149. ;; addl-chars are characters to be temporarily considered as alphanumerical
  1150. (defun vip-looking-at-alpha (&optional addl-chars)
  1151.   (or (stringp addl-chars) (setq addl-chars ""))
  1152.   (if (eq vip-syntax-preference 'reformed-vi)
  1153.       (setq addl-chars (concat addl-chars "_")))
  1154.   (let ((char (char-after (point))))
  1155.     (if char
  1156.     (if (eq vip-syntax-preference 'strict-vi)
  1157.         (looking-at (concat "[" vip-strict-ALPHA-chars addl-chars "]"))
  1158.       (or (memq char
  1159.             ;; convert string to list
  1160.             (append (vconcat addl-chars) nil))
  1161.           (memq (char-syntax char)
  1162.             (append (vconcat vip-ALPHA-char-class) nil)))))
  1163.     ))
  1164.  
  1165. (defsubst vip-looking-at-separator ()
  1166.   (let ((char (char-after (point))))
  1167.     (if char
  1168.     (or (eq char ?\n) ; RET is always a separator in Vi
  1169.         (memq (char-syntax char)
  1170.           (append (vconcat vip-SEP-char-class) nil))))))
  1171.  
  1172. (defsubst vip-looking-at-alphasep (&optional addl-chars)
  1173.   (or (vip-looking-at-separator) (vip-looking-at-alpha addl-chars)))
  1174.  
  1175. (defsubst vip-skip-alpha-forward (&optional addl-chars)
  1176.   (or (stringp addl-chars) (setq addl-chars ""))
  1177.   (vip-skip-syntax
  1178.    'forward 
  1179.    (cond ((eq vip-syntax-preference 'strict-vi)
  1180.       "")
  1181.      (t vip-ALPHA-char-class ))
  1182.    (cond ((eq vip-syntax-preference 'strict-vi)
  1183.       (concat vip-strict-ALPHA-chars addl-chars))
  1184.      (t addl-chars))))
  1185.  
  1186. (defsubst vip-skip-alpha-backward (&optional addl-chars)
  1187.   (or (stringp addl-chars) (setq addl-chars ""))
  1188.   (vip-skip-syntax
  1189.    'backward 
  1190.    (cond ((eq vip-syntax-preference 'strict-vi)
  1191.       "")
  1192.      (t vip-ALPHA-char-class ))
  1193.    (cond ((eq vip-syntax-preference 'strict-vi)
  1194.       (concat vip-strict-ALPHA-chars addl-chars))
  1195.      (t addl-chars))))
  1196.  
  1197. ;; weird syntax tables may confuse strict-vi style
  1198. (defsubst vip-skip-all-separators-forward (&optional within-line)
  1199.   (vip-skip-syntax 'forward
  1200.            vip-SEP-char-class
  1201.            (or within-line "\n")
  1202.            (if within-line (vip-line-pos 'end))))
  1203. (defsubst vip-skip-all-separators-backward (&optional within-line)
  1204.   (vip-skip-syntax 'backward
  1205.            vip-SEP-char-class
  1206.            (or within-line "\n")
  1207.            (if within-line (vip-line-pos 'start))))
  1208. (defun vip-skip-nonseparators (direction)
  1209.   (let ((func (intern (format "skip-syntax-%S" direction))))
  1210.     (funcall func (concat "^" vip-SEP-char-class)
  1211.          (vip-line-pos (if (eq direction 'forward) 'end 'start)))))
  1212.  
  1213. (defsubst vip-skip-nonalphasep-forward ()
  1214.   (if (eq vip-syntax-preference 'strict-vi)
  1215.       (skip-chars-forward
  1216.        (concat "^" vip-strict-SEP-chars vip-strict-ALPHA-chars))
  1217.     (skip-syntax-forward
  1218.      (concat
  1219.       "^" vip-ALPHA-char-class vip-SEP-char-class) (vip-line-pos 'end))))
  1220. (defsubst vip-skip-nonalphasep-backward ()
  1221.   (if (eq vip-syntax-preference 'strict-vi)
  1222.       (skip-chars-backward
  1223.        (concat "^" vip-strict-SEP-chars vip-strict-ALPHA-chars))
  1224.     (skip-syntax-backward
  1225.      (concat
  1226.       "^" vip-ALPHA-char-class vip-SEP-char-class) (vip-line-pos 'start))))
  1227.  
  1228. ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
  1229. ;; Return the number of chars traveled.
  1230. ;; Either SYNTAX or ADDL-CHARS can be nil, in which case they are interpreted
  1231. ;; as an empty string.
  1232. (defun vip-skip-syntax (direction syntax addl-chars &optional limit)
  1233.   (let ((total 0)
  1234.     (local 1)
  1235.     (skip-chars-func (intern (format "skip-chars-%S" direction)))
  1236.     (skip-syntax-func (intern (format "skip-syntax-%S" direction))))
  1237.     (or (stringp addl-chars) (setq addl-chars ""))
  1238.     (or (stringp syntax) (setq syntax ""))
  1239.     (while (and (not (= local 0)) (not (eobp)))
  1240.       (setq local
  1241.         (+ (funcall skip-syntax-func syntax limit)
  1242.            (funcall skip-chars-func addl-chars limit)))
  1243.       (setq total (+ total local)))
  1244.     total
  1245.     ))
  1246.   
  1247.  
  1248.   
  1249.   
  1250. (provide 'viper-util)
  1251.  
  1252. ;;;  viper-util.el ends here
  1253.